import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:shortmovieapp/Common.dart';
import 'package:shortmovieapp/myController/RequestServerController.dart';
import 'shareView.dart';
import 'package:shortmovieapp/data/localData.dart';

class mySearchPage extends StatefulWidget {
  //final Object? arguments;

  const mySearchPage({super.key /*, this.arguments*/});

  @override
  State<mySearchPage> createState() => _mySearchPageState();
}

class _mySearchPageState extends State<mySearchPage> {

  List<String>? searchTipsData;
  List<MovieItemData>? itemDatas;

  String? searchingKey;
  
  @override
  Widget build(BuildContext context) {
    return Container(
        padding: const EdgeInsets.fromLTRB(10, 40, 10, 0),
        child: Scaffold(
            appBar: AppBar(
              backgroundColor: Colors.transparent,
              title:  MySeachBar(
                onSearchKeyUpdate: (newSearchKey){
                      Debug.log("on search key update:$newSearchKey");
                      searchingKey = newSearchKey;
                },),
              actions: [
                TextButton(
                    onPressed: () {
                      Debug.log("on click search");
                      if(searchingKey == null){
                        
                      Debug.log("nothing to search");
                        return;
                      }
                      setState(() {
                        //TODO. 请求网络获得影片数据
                        itemDatas = RequestServerController()
                            .getSearchMovies(searchingKey!);
                      });
                    },
                    child: const Text("搜索"))
              ],
            ),
            body: Stack(children: [
              //searchBar(),
              searchTipsData != null
                  ? Column(
                      children: seachTipsList(searchTipsData!, (index) {
                        Debug.log("pressed index:$index");
                      }),
                    )
                  : const Row(),
              itemDatas != null
                  ? MovieItemsGrid(itemDatas: itemDatas!)
                  : const Row(),
            ])));
  }

//搜索提示列表
  List<Widget> seachTipsList(
      List<String> resultData, Function(int)? onPressItem) {
    var list = List<Widget>.empty();
    for (int i = 0; i < resultData.length; i++) {
      list.add(searchTipsItem(
          index: i, content: resultData[i], onPressed: onPressItem));
    }
    return list; //resultData.map((e) => seachResultItem(,e,onPressItem)).toList();
  }
}

//搜索提示Item
class searchTipsItem extends StatelessWidget {
  final int index;
  final Function(int)? onPressed;
  final String content;
  const searchTipsItem(
      {super.key, required this.index, this.content = "", this.onPressed});

  @override
  Widget build(BuildContext context) {
    return TextButton(
        onPressed: () => onPressed != null ? onPressed!(index) : null,
        child: Row(
          children: [const graySearchIcon(), Text(content)],
        ));
  }
}

//灰色搜索Icon
class graySearchIcon extends StatelessWidget {


  const graySearchIcon({super.key});

  @override
  Widget build(BuildContext context) {
    return const Icon(
      size: 12,
      Icons.search,
      color: Colors.grey,
    );
  }
}

//搜索栏
class MySeachBar extends StatefulWidget {

  final Function(String?)? onSearchKeyUpdate;
  const MySeachBar({super.key,this.onSearchKeyUpdate});

  @override
  State<MySeachBar> createState() => _MySeachBarState();
}

class _MySeachBarState extends State<MySeachBar> {
  String? seachingKey;

  @override
  Widget build(BuildContext context) {
    List<Widget> childs = [];
    childs.add(const graySearchIcon());
    childs.add(
      Container(padding: const EdgeInsets.only(left : 5),
        alignment: Alignment.centerLeft,
        child: Text(
        seachingKey != null ? seachingKey! : "搜索",
        style: const TextStyle(fontSize: 12, color: Colors.white38),
      )),
    );

    if (seachingKey != null) {
      childs.add(IconButton(
          onPressed: () {
            setState(() {
              seachingKey = null;

              widget.onSearchKeyUpdate?.call(seachingKey);
            });
          },
          icon: const Icon(Icons.close)));
    }

    return Container( 
    
      padding: const EdgeInsets.fromLTRB(10, 5, 10, 5),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(10.0), color: Colors.white10),
      alignment: Alignment.center,
      width: 300,
      height: 30,
      child: Row(children: childs),
    );
  }
}
